package com.rs.util;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZpiUtils {

    /**
     * 压缩
     */

    public static void zip(String input, String output, String name) throws Exception {
        //        //要生成的压缩文件
        File zipfile=new File(output);
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

        String[] paths = input.split("\\|");
        File[] files = new File[paths.length];
        byte[] buffer = new byte[1024];
        for (int i = 0; i < paths.length; i++) {
            files[i] = new File(paths[i]);
        }
        for (int i = 0; i < files.length; i++) {
            FileInputStream fis = new FileInputStream(files[i]);
            out.putNextEntry(new ZipEntry(files[i].getName()));

            int len;
            // 读入需要下载的文件的内容，打包到zip文件
            while ((len = fis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.closeEntry();
            fis.close();
        }
        out.close();
    }



    public static void main(String[ ] args){
        try {
            zip("C:\\Users\\wangzz\\Desktop\\22222.zip|C:\\Users\\wangzz\\Desktop\\11111.zip","D:\\ideaworkspace\\exam\\target\\exam\\\\upload\\temp\\aa.zip","testzzz");
//            zip("E:\\Testzip\\test\\ytt.html","E:\\Test.zip","testytt");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
